﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace DIS.Infrastructure.Entities.Repository
{
    [Table("T_LongHaul")]
    public class T_LongHaul : IEntity<T_LongHaul>
    {
        [Key]
        [Column(TypeName = "int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int ProjectId { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string UserName { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(10)]
        public string Year { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(10)]
        public string Month { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(20)]
        public string StartDate { get; set; }

        public int TotalDays { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(20)]
        public string EndDate { get; set; }

        public decimal TotalFee { get; set; }

        public int InvoiceCount { get; set; }

        public decimal AllowanceMoney { get; set; }

        public int Status { get; set; }


        public override T_LongHaul Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_LongHaul Find(System.Linq.Expressions.Expression<Func<T_LongHaul, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.LongHaul.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.ProjectId = item.ProjectId;
                    this.Year = item.Year;
                    this.Month = item.Month;
                    this.UserName = item.UserName;
                    this.StartDate = item.StartDate;
                    this.EndDate = item.EndDate;
                    this.TotalDays = item.TotalDays;
                    this.TotalFee = item.TotalFee;
                    this.InvoiceCount = item.InvoiceCount;
                    this.Status = item.Status;
                    this.TotalDays = item.TotalDays;
                    this.AllowanceMoney = item.AllowanceMoney;

                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_LongHaul, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.LongHaul.Any(where);
            }
        }

        public override T_LongHaul FindLast(System.Linq.Expressions.Expression<Func<T_LongHaul, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.LongHaul.OrderByDescending(c => c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.ProjectId = item.ProjectId;
                    this.Year = item.Year;
                    this.Month = item.Month;
                    this.UserName = item.UserName;
                    this.StartDate = item.StartDate;
                    this.EndDate = item.EndDate;
                    this.TotalFee = item.TotalFee;
                    this.TotalDays = item.TotalDays;
                    this.InvoiceCount = item.InvoiceCount;
                    this.Status = item.Status;
                    this.TotalDays = item.TotalDays;
                    this.AllowanceMoney = item.AllowanceMoney;
                    return this;
                }
                return null;
            }
        }
    }
}
